package com.clj.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.clj.ext.MainViewTableModel_clj;
import com.clj.ext.MainViewTable_clj;
import com.clj.handler.MainViewHandler_clj;
import com.clj.req.StudentRequest_clj;
import com.clj.res.TableDTO_clj;
import com.clj.service.StudentService_clj;
import com.clj.service.impl.StudentServiceCljImpl_clj;
import com.clj.util.DimensionUtil;

/**
 * Author: Harry Chen
 * Date: 2023/02/24 15:52
 * Description: 主页面
 */
public class MainView_clj extends JFrame {
    JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JButton addBtn = new JButton("增加");
    JButton updateBtn = new JButton("修改");
    JButton delBtn = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton searchBtn = new JButton("查询");

    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton preBtn = new JButton("上一页");
    JButton nextBtn = new JButton("下一页");
    MainViewTable_clj mainViewTableClj = new MainViewTable_clj();

    private int pageNow = 1;//当前是第几页，默认显示第一页
    private int pageSize = 10;//一页显示多少条数据库记录

    MainViewHandler_clj mainViewHandlerClj;

    public MainView_clj() {
        setTitle("陈林杰-成绩管理系统主界面for MySql");
        Container contentPane = getContentPane();
        Rectangle bounds = DimensionUtil.getBounds();
        pageSize = Math.floorDiv(bounds.height, 33);

        mainViewHandlerClj = new MainViewHandler_clj(this);
        //放置北边的组件
        layoutNorth(contentPane);
        //设置中间的表格
        layoutCenter(contentPane);
        //防止南边的组件
        layoutSouth(contentPane);

        //根据屏幕大小来设置主页面大小


        setBounds(bounds);
        //设置窗体完全充满整个屏幕的大小

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        //ImageIcon icon = new ImageIcon("picture\\clj.jpg");
        //setIconImage(icon.getImage()); //给窗体设置图标方法
        URL imgUrl = LoginView_clj.class.getClassLoader().getResource("clj.jpg");
        ImageIcon icon = new ImageIcon(new ImageIcon(imgUrl).getImage());
        setIconImage(icon.getImage()); //给窗体设置图标方法
        setVisible(true);
        setLocationRelativeTo(null);//居中
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭退出程序
        setResizable(true);//大小可改变
    }

    private void layoutCenter(Container contentPane) {

        TableDTO_clj dto = getTableDTO();
        MainViewTableModel_clj mainViewTableModelClj = MainViewTableModel_clj.assembleModel(dto.getData());
        mainViewTableClj.setModel(mainViewTableModelClj);
        mainViewTableClj.renderRule();
        JScrollPane jScrollPane = new JScrollPane(mainViewTableClj);//展示列头
        contentPane.add(jScrollPane, BorderLayout.CENTER);
        showPreNext(dto.getTotalCount());
    }

    private TableDTO_clj getTableDTO() {
        StudentService_clj studentServiceClj = new StudentServiceCljImpl_clj();
        StudentRequest_clj request = new StudentRequest_clj();
        request.setPagenOW(pageNow);
        request.setPageSize(pageSize);//retrieveStudents(request);
        request.setSearchKey(searchTxt.getText().trim());
        TableDTO_clj tableDTOClj = studentServiceClj.retrieveStudents(request);
        return tableDTOClj;
    }


    private void layoutNorth(Container contentPane) {
        //增加聆听事件
        addBtn.addActionListener(mainViewHandlerClj);
        updateBtn.addActionListener(mainViewHandlerClj);
        delBtn.addActionListener(mainViewHandlerClj);
        searchBtn.addActionListener(mainViewHandlerClj);
        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel, BorderLayout.NORTH);
    }

    private void layoutSouth(Container contentPane) {
        preBtn.addActionListener(mainViewHandlerClj);
        nextBtn.addActionListener(mainViewHandlerClj);
        southPanel.add(preBtn);
        southPanel.add(nextBtn);
        contentPane.add(southPanel, BorderLayout.SOUTH);
    }

    //设置上一页下一页是否可见
    private void showPreNext(int totalCount) {
        if (pageNow == 1) {
            preBtn.setVisible(false);
        } else {
            preBtn.setVisible(true);
        }
        int pageCount = 0;//总共有多少页
        if (totalCount % pageSize == 0) {
            pageCount = totalCount / pageSize;
        } else {
            pageCount = totalCount / pageSize + 1;
        }
        if (pageNow == pageCount) {
            nextBtn.setVisible(false);
        } else {
            nextBtn.setVisible(true);
        }
    }

    public static void main(String[] args) {
        new MainView_clj();
    }


    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public void reloadTable() {
        TableDTO_clj dto = getTableDTO();
        MainViewTableModel_clj.updateModel(dto.getData());
        mainViewTableClj.renderRule();
        showPreNext(dto.getTotalCount());
    }

    public int[] getSelectedStudentIds() {
        int[] selectedRows = mainViewTableClj.getSelectedRows();
        int[] ids = new int[selectedRows.length];
        for (int i = 0; i < selectedRows.length; i++) {
            int rowIndex = selectedRows[i];
            Object idObj = mainViewTableClj.getValueAt(rowIndex, 0);
            ids[i] = Integer.valueOf(idObj.toString());
        }
        return ids;
    }
}
